﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace carsitem.StockManagement
{
    public partial class StockManagementEntyForm : Form
    {
        //皮肤组件声明
        Sunisoft.IrisSkin.SkinEngine se = null;
        string SkinName = "SteelBlack";

        public StockManagementEntyForm()
        {
            InitializeComponent();

            //皮肤组件引用
            se = new Sunisoft.IrisSkin.SkinEngine();
            se.SkinAllForm = true;
            se.SkinFile = "skin/" + SkinName + ".ssk";
        }

        //打开窗口更新表格
        private void StockManagementEntyForm_Load(object sender, EventArgs e)
        {
            string sql1 = string.Format("select * from  StockManagement");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql1);
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = dt;
        }

        //退出按钮
        private void Eext_Click(object sender, EventArgs e)
        {
            carsitem.MainInterface.MainInterfaceForm mainInterfaceForm = new carsitem.MainInterface.MainInterfaceForm();//把form1传给form2
            mainInterfaceForm.Show();
            this.Close();//把自己隐藏了
        }

        //添加按钮
        private void Add_Click(object sender, EventArgs e)
        {
            try//捕捉全空文本框异常
            {
                //sql添加语句
                string sql = string.Format("insert into  StockManagement(OrderQuantity,DateOrder,TermsPayment,Note,WhetherToReturn,GoodsNumber) values({0},'{1}','{2}','{3}','{4}',{5})", int.Parse(this.textOrderQuantity.Text), this.dateTimeDateOrder.Value, this.comboTermsPayment.Text, this.textNote.Text, this.comboWhetherToReturn.Text, int.Parse(this.textGoodsNumber.Text));

                //判断是否成功
                if (DBHelper.Instance().ExcuteSql(sql))
                {
                    MessageBox.Show("添加成功！");
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }

                //清除文本框数据
                this.textOrderNumber.Clear();
                this.textSupplierNumber.Clear();
                this.textGoodsNumber.Clear();
                this.textOrderQuantity.Clear();
                this.textNote.Clear();
            }
            catch (Exception)
            {
                MessageBox.Show("请输入要添加的数据");//文本框中无数据弹出
            }
             
        }

        //数字验证
        public bool IsNumberic(string oText)
        {
            string pattern = @"^\d+(\.\d)?$";
            if (oText != "")
            {
                if (!Regex.IsMatch(oText, pattern))
                    MessageBox.Show("请输入数字！");
            }
            return true;
        }

        //非空验证
        public bool Null(string oText)
        {
            if (oText == "")
            {
                MessageBox.Show("必须填写！");
            }
            return true;
        }

        //表单验证
        private void textOrderNumber_TextChanged(object sender, EventArgs e)//进货单号
        {
            string oText = textOrderNumber.Text.Trim();
            IsNumberic(oText);
            Null(oText);
        }

        private void textSupplierNumber_TextChanged(object sender, EventArgs e)//供商编号
        {
            string oText = textSupplierNumber.Text.Trim();
            IsNumberic(oText);
            Null(oText);
        }

        private void textGoodsNumber_TextChanged(object sender, EventArgs e)//货物编号
        {
            string oText = textGoodsNumber.Text.Trim();
            IsNumberic(oText);
            Null(oText);
        }

        private void textOrderQuantity_TextChanged(object sender, EventArgs e)//订货数量
        {
            string oText = textOrderQuantity.Text.Trim();
            IsNumberic(oText);
            Null(oText);
        }
    }
}
